from vBaum import *
from math import *
from random import random
import ctypes

# based on a script by Stanislaw Adaszewski, 2013

scale=10

def fixLong(n):
	if n >= 1<<31:
		n -= 1<<32
	return ctypes.c_uint32(n).value
	
pairs = [(fixLong(0x00ff00ff), fixLong(0xff00ffff)),
(fixLong(0x0000ffff), fixLong(0xff0000ff)),
(fixLong(0xff00ffff), fixLong(0x00ff00ff)),
(fixLong(0xff0000ff), fixLong(0x0000ffff))]

pair = []

def createBasePairStrand(fPos):
	sphere = Sphere(1.7)
	if fPos < 0.5: sphere.setColor(pair[0])
	else: sphere.setColor(pair[1])
	return sphere
	
def createSphere(fPos):
	sphere = Sphere(3.0)
	sphere.setColor(fixLong(0x999900ff))
	return sphere

splinePoints1 = []
splinePoints2 = []
basePairStrands = Volume()
for i in range(0, 100):
	angle = i * pi / 40
	if i % 6 == 0:
		v1 = Vector3(sin(angle)*2, cos(angle)*2, i/10)*scale
		v2 = Vector3(sin(angle+pi)*2, cos(angle+pi)*2, i/10)*scale
		splinePoints1.append(v1)
		splinePoints2.append(v2)
		pair = pairs[int(random() * 4)]
		basePairStrand = Spline([v1, v2], False, createBasePairStrand)
		basePairStrands = basePairStrands.union(basePairStrand)

print("creating big spline")		
spline1 = Spline(splinePoints1, False, createSphere)
print("creating big spline 2")	
spline2 = Spline(splinePoints2, False, createSphere)
dna = spline1.union(spline2)
dna = dna.union(basePairStrands)

dna.exportAsPly("dna_strand.ply", 10, 5)